package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.service.UserAbilityService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.UserAbilityView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @ClassName UserAbilityController
 * @Description TODO
 * @Date 2020/11/29 14:50
 * @Author maixy
 */
@RestController
@RequestMapping("/v1/user/ability")
public class UserAbilityController extends ApiController {
    @Resource
    private UserAbilityService userAbilityService;

    /**
     * 保存用户对课程的能力
     *
     * @param request -- activityId 活动id
     *                -- contentLevel 难度
     *                -- contentType 类型
     * @return --
     */
    @Privilege
    public ApiResponse saveOrUpdateAbility(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Integer contentLevel = request.getDataParamAsInt("contentLevel");
        Integer contentType = request.getDataParamAsInt("contentType");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        if (null == activityId || null == contentLevel || null == contentType) {
            throw new TipException("参数缺失");
        }
        userAbilityService.saveOrUpdateAbility(activityId, contentLevel, contentType, userId);
        return ApiResponse.ok();
    }

    /**
     * 查看学员在课程中的标签
     *
     * @param request activityId -- 活动id
     * @return --
     */
    @Privilege
    public ApiResponse abilityDetail(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        UserAbilityView userAbilityView = userAbilityService.abilityDetail(activityId, userId);
        return ApiResponse
                .ok()
                .addValueToData("userAbilityView", userAbilityView);
    }
}
